// pages/cf-list/cf-list.js
var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var goTop = require('../widget/goTop/goTop');

var pageSetting = {
  data: {
    show: false,
    selectedIndex: 0,
    pageShow: 0,
    totalAmount: 0,
    skus: []
  },
  onLoad: function (option) {
    this.data.id = option.id;
    this.setData({
      id: this.data.id
    });
    this.loadData();
  },
  loadData: function () {
    var that = this;
    var data = {
      "zp:zc_api:act:sku":
      { "id": that.data.id }
    };
    modal.loading();
    request.post({
      path: '/wxapp_api/api'
    }, data).then(function (res) {
      modal.hide();
      if (res['zp:zc_api:act:sku'].ret == 0) {
        for (var i = 0; i < res['zp:zc_api:act:sku'].data.skus.length; i++) {
          if (that.data.selectedIndex == i && res['zp:zc_api:act:sku'].data.skus[i].inventory <= 0) {
            that.data.selectedIndex = i + 1;
          }
          res['zp:zc_api:act:sku'].data.skus[i].pickNum = 1;
          that.data.skus.push(res['zp:zc_api:act:sku'].data.skus[i]);
        }
        that.data.pageShow = 1;
        that.setData({
          selectedIndex: that.data.selectedIndex,
          skus: that.data.skus,
          pageShow: that.data.pageShow
        });
      } else {
        modal.toast(res['zp:zc_api:act:sku']['msg']);
      }
    });
  },
  changeSelected: function (ev) {
    var targetIndex = ev.currentTarget.dataset.index;
    if (this.data.skus[targetIndex].inventory > 0) {
      this.data.selectedIndex = targetIndex;
      this.data.totalAmount = this.data.skus[this.data.selectedIndex].price * this.data.skus[this.data.selectedIndex].pickNum / 100;
      this.setData({
        selectedIndex: this.data.selectedIndex,
        totalAmount: this.data.totalAmount
      });
    }
  },
  reduceNum: function (ev) {
    var that = this;
    if (that.data.selectedIndex == ev.currentTarget.dataset.index) {
      if (that.data.skus[that.data.selectedIndex].pickNum > 1) {
        that.data.skus[that.data.selectedIndex].pickNum -= 1;
        that.data.totalAmount = that.data.skus[that.data.selectedIndex].price * that.data.skus[that.data.selectedIndex].pickNum / 100;
      }
      that.setData({
        skus: that.data.skus,
        totalAmount: that.data.totalAmount
      });
    }
  },
  increaseNum: function (ev) {
    var that = this;
    if (that.data.selectedIndex == ev.currentTarget.dataset.index) {
      if (that.data.skus[that.data.selectedIndex].pickNum <= parseInt(that.data.skus[that.data.selectedIndex].inventory) - 1) {
        that.data.skus[that.data.selectedIndex].pickNum += 1;
        that.data.totalAmount = that.data.skus[that.data.selectedIndex].price * that.data.skus[that.data.selectedIndex].pickNum / 100;
      }
      that.setData({
        skus: that.data.skus,
        totalAmount: that.data.totalAmount
      });
    }
  },
  inputNum: function (ev) {
    var that = this;
    if (that.data.selectedIndex == ev.currentTarget.dataset.index) {
      var targetVal = parseInt(ev.detail.value);
      if (isNaN(targetVal) || targetVal < 1) {
        targetVal = 1;
      }
      if (targetVal > parseInt(that.data.skus[that.data.selectedIndex].inventory)) {
        targetVal = parseInt(that.data.skus[that.data.selectedIndex].inventory);
      }
      that.data.skus[that.data.selectedIndex].pickNum = targetVal;
      that.data.totalAmount = that.data.skus[that.data.selectedIndex].price * targetVal / 100;
      that.setData({
        skus: that.data.skus,
        totalAmount: that.data.totalAmount
      });
    }
  },
};
Page(pageSetting)
